<?php
namespace app\controller;
use app\AuthController;
use think\facade\Request; //在没有使用依赖注入的场合，可以通过Facade机制来静态调用请求对象的方法
use think\facade\Db;      //使用Db数据库对象时需要引入的方法
use think\facade\View;    //引入视图模版解析类
use think\facade\Validate;//引入验证器类
use think\facade\Session;
use think\facade\Route;

use app\BaseInfoController as BaseInfo;

class Link extends AuthController
{
# 用户链接管理综合功能
protected $middleware = [
'check_user_or_null' => ['only' => [  // 只有
    // 'link',         // 展示链接空间
] ],'check_user_or_jump' => ['only' => [ // 只有
    'addhyperlink', // 添加链接
    'index',        // 链接空间管理
    'hyperlink',    // 编辑链接首页
] ],'check_user_or_json' => ['except' => [ // 除去
    'link',         // 展示链接空间
    'addhyperlink', // 添加链接
    'index',        // 链接空间管理
    'hyperlink',    // 编辑链接首页
] ], ];

##################################################################### 【展示链接空间】 ######################################################################
# 查看梦幻空间站链接空间
public function link(){
    # 获取登录用户信息和链接LID
    $user = $this->get_user();
    $sid  = $this->request->param('spaceId/s'); // 获取传入的链接空间sid

    # 根据链接LID找到链接 公开 / 发布
    $link_space = Db::name('link_space')->where('lid',$sid)->where([['state','>=',2],['state','<=',3]])->findOrEmpty();

    # 根据文章UID和登录用户找到用户自己的文章 回收 / 私密 / 退回
    if(empty($link_space) && !is_null($user)){
        $link_space = Db::name('link_space')->where('lid',$sid)->where('uid',$user['uid'])->findOrEmpty();
    }

    # 传入公共变量
    $site_info = BaseInfo::siteInfo();
    View::assign([
        'user' => $user,
        'site_info'     => $site_info,  // 网站信息
        'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
        'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
        'menu' => null, // 首页导航ID
    ]);

    # 渲染模板并输出
    if(!empty($link_space)){ // 找到了链接
        # 根据链接空间获取链接内容
        $result = Db::name('link_group')->where('uid',$link_space['uid'])->where('far_id',$link_space['id'])->field('id,name')->order('link_order desc')->select()->toArray();
        $link_gp_id = Db::name('link_group')->where('uid',$link_space['uid'])->where('far_id',$link_space['id'])->column('id');
        $links = Db::name('link')->where('user_id',$link_space['uid'])->where('far_id','in',$link_gp_id)->order('link_order desc')->select()->toArray();
        foreach($result as $key => $value){
            foreach($links as $k => $vol){
                if($vol['far_id']==$value['id']){
                    $result[$key]['class'][] = $vol;
                    unset($links[$k]);
                }
            }
        }
        # 结果传入模板
        View::assign([
            'link_space' => $link_space, // 链接空间
            'link_info'  => $result,     // 链接内容
        ]);
        # 根据不同模板加载不同页面
        if($link_space['link_model']=='pic1_link'){

            return View::fetch('pic1_link');

        }else if($link_space['link_model']=='pic2_link'){

            return View::fetch('pic2_link');

        }else if($link_space['link_model']=='txt2_link'){

            return View::fetch('txt2_link');

        }else{

            return View::fetch('txt1_link');

        }
    }else{ // 没找到链接
        return View::fetch('mz_blog/404');
    }
}
##########################################################################################################################################################




















####################################################################### 【添加链接】 ########################################################################
# 添加文字超链接主页
public function addhyperlink(){
    # 获取登录用户信息
    $user = $this->get_user();
    #检查是否有POST传入从而决定显示页面还是进行添加操作
    if($this->request->isPost()){
        $name = $this->request->post('name/s');
        if(empty($name))
            $this -> skip('标题不能为空哦','javascript:history.go(-1)', 0, 1);
        #获取排序link_order最大值
        $link_order = Db::name('link')->where('user_id',$user['uid'])->max('link_order');
        $link_order += 1;
        $data = [
            'user_id' => $user['uid'],
            'title' => $name,
            'url'   => $this->request->post('url/s'),
            'description' => $this->request->post('des/s'),
            'link_order' => (int)$link_order,
        ];
        $result = Db::name('link')->insert($data);
        if($result)
            $this->skip('添加成功','/link/addhyperlink',1,1);
        else
            $this->skip('添加失败','javascript:history.go(-1)',0,1);
    }else{
        View::assign([
            'user'  => $user,
            'site_info'     => BaseInfo::siteInfo(),  // 网站信息
            'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
            'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
            'menu' => null,                            // 导航ID
        ]);
        return View::fetch('hyperlink_txt_add');
    }
}
# JSON添加文字超链接
public function json_add_hyperlink(){
    # 获取登录用户信息
    $user = $this->get_user();
    if($this->request->isPost()){
        $data1 = $this->request->post('data1/s');
        $data2 = $this->request->post('data2/s');
        $data3 = $this->request->post('data3/s');
    }
    if(empty($data1)||empty($data2))
        return json(['state'=>'error','info'=>'标题和URL不能为空']);
    #获取排序link_order最大值
    $link_order = Db::name('link')->where('user_id',$user['uid'])->max('link_order');
    $link_order++;
    $data = ['user_id'=>$user['uid'], 'title'=>$data1, 'url'=>$data2, 'description'=>$data3, 'link_order'=>$link_order];
    $result = Db::name('link')->insertGetId($data);
    if($result){
        $res['state'] = 'success';
        $res['info'] = '添加成功';
        return json($res);
    }else{
        $res['state'] = 'error';
        $res['info'] = '添加失败';
        return json($res);
    }
}
# 上传文章封面图
public function upl_cover_img(){
    if($this->request->isPost()){
        $user = Session::get('user');
        $lid  = $this->request->post('lid/s');
        $file = request()->file('img');
        $savename = \think\facade\Filesystem::disk('public')->putFile( '', $file);
        $saveurl  = '/storage/'.$savename;
        $image = \think\Image::open('.'.$saveurl);
        $image->thumb(300,168,\think\Image::THUMB_FIXED)->save('.'.$saveurl);
        $is_ok = Db::name('link')->where('id',$lid)->where('user_id',$user['uid'])->update(['cover_img'=>$saveurl]);
        if($is_ok){
            $this -> skip('更换图片成功',"javascript:window.opener=null;window.open('','_self');window.close();",1,1);
        }else{
            $this -> skip('更换图片失败',"javascript:window.opener=null;window.open('','_self');window.close();",0,1);
        }
    }
}
##########################################################################################################################################################




















############################################################# 统计链接量 ##################################################################################
# 统计链接空间链接数量
protected function statistics_linkspace_quantity($uid){
    $link_space = Db::name('link_space')->where('uid',$uid)->field('id')->select()->toArray();
    $link_group = Db::name('link_group')->where('uid',$uid)->field('id,far_id')->select()->toArray();
    $links      = Db::name('link')->where('user_id',$uid)->where('far_id','<>',null)->column('far_id');
    foreach($link_space as $key => $value){
        $value['num'] = 0;
        foreach($link_group as $k => $vol){
            if($vol['far_id'] == $value['id']){
                foreach($links as $a => $v){
                    if($v == $vol['id']){
                        $value['num']++;
                        unset($links[$a]);
                    }
                }
                unset($link_group[$k]);
            }
        }
        $link_space[$key]['num'] = $value['num'];
    }
    foreach($link_space as $key => $value)
        Db::name('link_space')->where('id',$value['id'])->update(['linknum'=>$value['num']]);

    if(!empty($link_group) && isset($link_space[0]['id'])){
        foreach($link_group as $k => $vol){
            Db::name('link_group')->where('id',$vol['id'])->update(['far_id'=>$link_space[0]['id']]);
            unset($link_group[$k]);
        }
    }
    if(!empty($links)){
        foreach($links as $k => $vol){
            Db::name('link')->where('id',$vol['id'])->update(['far_id'=>null]);
            unset($links[$k]);
        }
    }
}
##########################################################################################################################################################





############################################################# 个人链接管理首页 - 链接空间 ###################################################################
# 个人链接管理首页
public function index(){
    # 获取登录用户信息
    $user = $this->get_user();
    # 统计链接空间数量
    $this -> statistics_linkspace_quantity($user['uid']);

    # 获取链接空间
    $link_space = Db::name('link_space')->where('uid',$user['uid'])->order('link_order desc')->select()->toArray();

    # 获取全部链接数量和未分组链接数量
    $all = Db::name('link')->where('user_id',$user['uid'])->count();
    $nil = Db::name('link')->where('user_id',$user['uid'])->where('far_id',null)->count();

    # 传入公共变量
    $site_info = BaseInfo::siteInfo();
    View::assign([
        'user'          => $user,                  // 登录用户信息
        'link_space'    => $link_space,            // 链接空间
        'all'           => $all,                   // 全部
        'nil'           => $nil,                   // 未入集
        'site_info'     => BaseInfo::siteInfo(),  // 网站信息
        'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
        'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
        'menu' => null,                            // 导航ID
    ]);
    return View::fetch();
}
##########################################################################################################################################################




















################################################### 【链接空间发布】 获取当前、设置状态、取消发布、设置模板 #######################################################
# JSON获取链接空间状态
public function json_get_hyperspace_state(){
    if($this->request->isPost()){
        # 获取登录用户信息
        $user = $this->get_user();
        # 添加操作
        $id = $this->request->post('id/s');
        $result = Db::name('link_space')->where('uid',$user['uid'])->where('id',$id)->field('id,title,link_model,state')->findOrEmpty();
        if($result){
            if($result['link_model']=='mz_link')
                $result['link_model'] = 1;
            else if($result['link_model']=='pic1_link')
                $result['link_model'] = 2;
            else if($result['link_model']=='pic2_link')
                $result['link_model'] = 3;
            else if($result['link_model']=='txt2_link')
                $result['link_model'] = 4;
            else
                $result['link_model'] = 0;
            # 验证发布栏目的权限
            if(in_array('Q64',explode(',',$user['limit'])))
                $result['limit_description'] = '无需审核即可发布链接';
            else
                $result['limit_description'] = '发布链接需要审核，可以在个人中心认证链接发布免审核';
            return json(['state'=>'success','info'=>$result]);
        }else{
            return json(['state'=>'error','info'=>'链接空间不存在']);
        }
    }
}
# JSON设置链接空间状态
public function json_set_hyperspace_state(){
    if($this->request->isPost()){
        # 获取登录用户信息
        $user = $this->get_user();
        # 设置操作
        $lid = $this->request->post('lid/d');
        $sta = $this->request->post('sta/d');
        if($sta>0 && $sta<4){
            if($sta==3){
                $rel_order = Db::name('link_space')->where('state',3)->max('rel_order');
                $rel_order++;
                if(in_array('Q64',explode(',',$user['limit']))){
                    $des = '发布成功';
                    $state = 3;
                }else{
                    $des = '设置成功，等待审核';
                    $state = 5;
                }
                $result = Db::name('link_space')->where('uid',$user['uid'])->where('id',$lid)->update(['state'=>$state,'rel_order'=>$rel_order]);
            }else{
                $des = '设置成功';
                $result = Db::name('link_space')->where('uid',$user['uid'])->where('id',$lid)->update(['state'=>$sta]);
            }
        }else{
            return json(['state'=>'error','info'=>'传值不合法']);
        }
        # 设置结果
        if($result){
            return json(['state'=>'success','info'=>$des]);
        }else{
            return json(['state'=>'error','info'=>'设置失败或设置值无变化']);
        }
    }
}
# JSON取消链接空间发布
public function json_ret_release_linkspace(){
    if($this->request->isPost()){
        # 获取登录用户信息
        $user = $this->get_user();
        $id  = $this->request->post('id/s');
        $state = Db::name('link_space')->where('uid',$user['uid'])->where('id',$id)->value('state');
        if($state==3 || $state==5){
            $result = Db::name('link_space')->where('uid',$user['uid'])->where('id',$id)->update(['state'=>1]);
            if($result)
                return json(['state'=>'success','info'=>'取消成功']);
            else
                return json(['state'=>'error','info'=>'莫名原因导致取消失败']);
        }else{
            return json(['state'=>'error','info'=>'链接并没有处于发布状态哦']);
        }
    }
}
# JSON设置链接空间模板
public function json_set_linkspace_model(){
    if($this->request->isPost()){
        $uid = Session::get('user.uid');
        # 验证并删除链接
        $id = $this->request->post('id/d');
        $dt = $this->request->post('dt/d');
        if($dt==1)
            $model = 'mz_link';
        else if($dt==2)
            $model = 'pic1_link';
        else if($dt==3)
            $model = 'pic2_link';
        else if($dt==4)
            $model = 'txt2_link';
        else
            return json(['state'=>'error','info'=>'模板不存在']);
        $result = Db::name('link_space')->where('uid',$uid)->where('id',$id)->update(['link_model'=>$model]);
        if($result){
            return json(['state'=>'success','info'=>'设置成功']);
        }else{
            return json(['state'=>'error','info'=>'设置失败']);
        }
    }
}
##########################################################################################################################################################





################################################# 【链接空间编辑】 新建、展示编辑、修改、删除、排序 ###############################################################
# JSON添加链接空间
public function json_add_hyperspace(){
    if($this->request->isPost()){
        $uid = Session::get('user.uid');
        #添加操作
        $name = $this->request->post('name/s');
        if(empty($name)){
            return json(['state'=>'error','info'=>'名称不能为空']);
        }
        if(mb_strlen($name) > 6){
            return json(['state'=>'error','info'=>'名称过长，不能超过6个字哦~']);
        }
        # 添加链接空间
        $max_order = Db::name('link_space')->where('uid',$uid)->max('link_order');
        $max_order++;
        $data = ['uid'=>$uid,'name'=>$name,'link_model'=>'mz_link','state'=>1,'link_order'=>$max_order];
        $result = Db::name('link_space')->insert($data);
        if($result){
            return json(['state'=>'success','info'=>'添加成功']);
        }else{
            return json(['state'=>'error','info'=>'不知道为啥就添加失败了']);
        }
    }
}
# JSON获取链接空间信息
public function json_get_linkspace(){
    $user_id = Session::get('user.uid');
    #查看链接
    $id = $this->request->param('id/d');
    $link = Db::name('link_space')->where('uid',$user_id)->where('id',$id)->withoutField('link_order,rel_order')->findOrEmpty();
    if(!empty($link)){
        if(empty($link['title'])) $link['title'] = '链接空间未命名';
        $link['zclass'] = Db::name('link_group')->where('uid',$user_id)->where('far_id',$id)->field('id,name')->order('link_order desc')->select()->toArray();
        return json(['state'=>'success','info'=>$link]);
    }else{
        return json(['state'=>'error','info'=>'链接不存在哦']);
    }
}
# JSON修改链接空间信息
public function json_upd_linkspace(){
    if($this->request->isPost()){
        $user_id = Session::get('user.uid');
        $data1  = $this->request->param('data1/s');
        $data2  = $this->request->param('data2/s');
        $data3  = $this->request->param('data3/s');
        $data4  = $this->request->param('data4/s');
        $data5  = $this->request->param('data5/d');
        $ishas = Db::name('link_space')->where('uid',$user_id)->where('id',$data5)->findOrEmpty();
        if(empty($ishas)) return json(['state'=>'error','info'=>'此链接空间不存在']);
        if($ishas['state']==3 && empty($data1)) return json(['state'=>'error','info'=>'发布的链接LID不能为空']);
        if(!empty($data1)){
            if(!preg_match("/^[a-zA-Z\s]+$/",$data1)) return json(['state'=>'error','info'=>'LID不符合格式，LID只能为英文字母']);
            if(empty($data2)) return json(['state'=>'error','info'=>'标题不能为空']);
            $ishas = Db::name('link_space')->where('id','<>',$data5)->where('lid',$data1)->findOrEmpty();
            if(!empty($ishas)) return json(['state'=>'error','info'=>'LID与网站中其他LID重复，请重新设置']);
        }
        if(empty($data3)) return json(['state'=>'error','info'=>'名称不能为空']);
        if(mb_strlen($data3) > 6) return json(['state'=>'error','info'=>'名称过长，不能超过6个字哦~']);
        $data = ['lid'=>$data1, 'title'=>$data2, 'name'=>$data3, 'description'=>$data4];
        $result = Db::name('link_space')->where('uid',$user_id)->where('id',$data5)->update($data);
        if($result)
            return json(['state'=>'success','info'=>'修改成功']);
        else
            return json(['state'=>'error','info'=>'修改失败或数据无变化']);
    }
}
# JSON删除链接空间
public function json_del_linkspace(){
    if($this->request->isPost()){
        $uid = Session::get('user.uid');
        # 验证并删除链接
        $id = $this->request->param('id/d');
        $ishas = Db::name('link_group')->where('uid',$uid)->where('far_id',$id)->find();
        if(!empty($ishas)){
            return json(['state'=>'error','info'=>'请先删除该链接空间下的子集链接分组']);
        }
        $result = Db::name('link_space')->where('uid',$uid)->where('id',$id)->delete();
        if($result){
            return json(['state'=>'success','info'=>'删除成功']);
        }else{
            return json(['state'=>'error','info'=>'删除失败']);
        }
    }
}
# JSON链接空间交换排序
public function json_exc_linkspace_order(){
    if($this->request->isPost()){
        $user_id = Session::get('user.uid');
        #检查传入的两个交换值并进行交换操作
        $id1 = $this->request->param('id1/d');
        $id2 = substr($this->request->param('id2/s'), 2);
        $path1 = Db::name('link_space')->where('uid',$user_id)->where('id',$id1)->value('link_order');
        $path2 = Db::name('link_space')->where('uid',$user_id)->where('id',$id2)->value('link_order');
        if(($path1 != null) && ($path2 != null)){
            Db::name('link_space')->where('id',$id1)->update(['link_order'=>$path2]);
            Db::name('link_space')->where('id',$id2)->update(['link_order'=>$path1]);
            return json(['state'=>'success']);
        }else{
            return json(['state'=>'error','info'=>'您的ID选项不正确哦']);
        }
    }
}
##########################################################################################################################################################





##################################### 【链接分组编辑】 新建分组、分组改名、删除分组、交换排序 #####################################################################
# JSON新建链接分组
public function json_add_linkgroup(){
    if($this->request->isPost()){
        $uid = Session::get('user.uid');
        #添加操作
        $farid  = $this->request->post('far/d');
        $name = $this->request->post('name/s');
        if(empty($name)){
            return json(['state'=>'error','info'=>'名称不能为空']);
        }
        $ishas = Db::name('link_space')->where('uid',$uid)->where('id',$farid)->findOrEmpty();
        if(empty($ishas)){
            return json(['state'=>'error','info'=>'此链接空间不存在']);
        }
        # 添加链接分组
        $max_order = Db::name('link_group')->where('uid',$uid)->max('link_order');
        $max_order++;
        $data = ['uid'=>$uid, 'far_id'=>$farid, 'name'=>$name, 'link_order'=>$max_order];
        $result = Db::name('link_group')->insert($data);
        if($result){
            return json(['state'=>'success','info'=>'添加成功，重新点击查看']);
        }else{
            return json(['state'=>'error','info'=>'不知道为啥就添加失败了']);
        }
    }
}
# JSON链接分组改名
public function json_upd_linkgroup(){
    if($this->request->isPost()){
        $uid = Session::get('user.uid');
        #改名
        $id   = $this->request->param('id/d');
        $name = $this->request->param('name/s');
        if(!empty($name)){
            $result = Db::name('link_group')->where('uid',$uid)->where('id',$id)->update(['name'=>$name]);
            if($result){
                return json(['state'=>'success','info'=>'修改成功']);
            }else{
                return json(['state'=>'error','info'=>'修改失败或数据无变化~']);
            }
        }else{
            return json(['state'=>'error','info'=>'名称不能为空哦~']);
        }
    }
}
# JSON删除链接分组
public function json_del_linkgroup(){
    if($this->request->isPost()){
        $uid = Session::get('user.uid');
        #删除该分组
        $id = $this->request->post('id/d');
        $ishas = Db::name('link_group')->where('uid',$uid)->where('id',$id)->findOrEmpty();
        if(!empty($ishas)){
            $result = Db::name('link_group')->where('id',$id)->delete();
            if($result){
                Db::name('link')->where('user_id',$uid)->where('far_id',$id)->update(['far_id'=>null]);
                return json(['state'=>'success','info'=>'删除成功']);
            }else{
                return json(['state'=>'error','info'=>'删除失败，请重试']);
            }
        }else{
            return json(['state'=>'error','info'=>'该链接分组不存在']);
        }
    }
}
# JSON链接分组交换排序
public function json_exc_linkgroup_order(){
    if($this->request->isPost()){
        $uid = Session::get('user.uid');
        #检查传入的两个交换值并进行交换操作
        $id1 = $this->request->param('id1/d');
        $id2 = substr($this->request->param('id2/s'), 10);
        $path1 = Db::name('link_group')->where('uid',$uid)->where('id',$id1)->value('link_order');
        $path2 = Db::name('link_group')->where('uid',$uid)->where('id',$id2)->value('link_order');
        if(($path1 != null) && ($path2 != null)){
            Db::name('link_group')->where('id',$id1)->update(['link_order'=>$path2]);
            Db::name('link_group')->where('id',$id2)->update(['link_order'=>$path1]);
            return json(['state'=>'success']);
        }else{
            return json(['state'=>'error','info'=>'您的ID选项不正确哦']);
        }
    }
}
##########################################################################################################################################################




















############################################################# 个人链接管理首页 - 编辑链接 ###################################################################
# 文字超链接主页
public function hyperlink(){
    # 获取登录用户信息
    $user = $this->get_user();

    # 获取链接空间
    $link_space = Db::name('link_space')->where('uid',$user['uid'])->order('link_order desc')->select()->toArray();

    # 获取全部链接数量和未分组链接数量
    $all = Db::name('link')->where('user_id',$user['uid'])->count();
    $nil = Db::name('link')->where('user_id',$user['uid'])->where('far_id',null)->count();

    # 获取当前要编辑的链接空间
    $sid = $this->request->param('spaceId/d');
    if($sid == -1){ // 未分组
        $result[0]['name']  = '未分组链接';
        $result[0]['class'] = Db::name('link')->where('user_id',$user['uid'])->where('far_id',null)->order('link_order desc')->select()->toArray();
    }
    if($sid == -2){ // 全部
        $result[0]['name']  = '全部链接';
        $result[0]['class'] = Db::name('link')->where('user_id',$user['uid'])->order('link_order desc')->select()->toArray();
    }
    if(empty($result)){ // 正常查找
        $result = Db::name('link_group')->where('uid',$user['uid'])->where('far_id',$sid)->order('link_order desc')->field('id,name')->select()->toArray();
        $res_id = Db::name('link_group')->where('uid',$user['uid'])->where('far_id',$sid)->column('id');
        $alllks = Db::name('link')->where('user_id',$user['uid'])->where('far_id','in',$res_id)->order('link_order desc')->select()->toArray();
        foreach($result as $key => $vol){
            $result[$key]['class'] = [];
            foreach($alllks as $k => $v){
                if($vol['id']==$v['far_id']){
                    $result[$key]['class'][] = $v;
                    unset($alllks[$k]);
                }
            }
        }
    }

    # 查看分组
    $group = Db::name('link_space')->where('uid',$user['uid'])->field('id,name')->order('link_order desc')->select()->toArray();
    $child = Db::name('link_group')->where('uid',$user['uid'])->field('id,far_id,name')->order('link_order desc')->select()->toArray();
    foreach($group as $key => $value){
        foreach($child as $k => $vol){
            if($vol['far_id'] == $value['id']){
                $group[$key]['class'][] = $vol;
                unset($child[$k]);
            }
        }
    }

    # 传入公共变量
    $site_info = BaseInfo::siteInfo();
    View::assign([
        'user'          => $user,                  // 登录用户信息
        'link_space'    => $link_space,            // 链接空间
        'result'        => $result,                // 主要链接内容
        'group'         => $group,                // 链接分组
        'all'           => $all,                   // 全部
        'nil'           => $nil,                   // 未入集
        'site_info'     => BaseInfo::siteInfo(),  // 网站信息
        'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
        'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
        'menu' => null,                            // 导航ID
    ]);
    return View::fetch('hyperlink');
}
##########################################################################################################################################################





########################################################## 【链接操作】 查看、修改、删除 交换排序、设置分组 ######################################################
# JSON查看链接
public function json_get_txtlink(){
    #查看链接
    $link = Db::name('link')->where('user_id',Session::get('user.uid'))->where('id',$this->request->param('id/d'))->findOrEmpty();
    if(!empty($link)){
        $link['state'] = 'success';
        return json($link);
    }else{
        return json(['state'=>'error','info'=>'链接不存在哦']);
    }
}
# JSON修改链接
public function json_upd_txtlink(){
    #修改链接
    $id  = $this->request->param('id/d');
    $validate = Validate::rule('id', 'number');
    if (!$validate->check(['id'  => $id]))
        return json(['state'=>'error','info'=>'您传入的ID值不合法']);
    $data   = ['title'=>input('param.t'),'url'=>input('param.u'),'description'=>input('param.d')];
    $result = Db::name('link')->where('user_id',Session::get('user.uid'))->where('id',$id)->update($data);//没修改任何数据返回0，修改了则返回受影响数据的条数
    if($result)
        return json(['state'=>'success','info'=>'修改成功']);
    else
        return json(['state'=>'error','info'=>'没有任何数据被修改']);
}
# JSON删除链接
public function json_del_txtlink(){
    #验证并删除链接
    $id  = $this->request->param('id/d');
    $validate = Validate::rule('id', 'number');
    if (!$validate->check(['id'  => $id]))
        return json(['state'=>'error','info'=>'您传入的ID值不合法']);
    $result = Db::name('link')->where('user_id',Session::get('user.uid'))->where('id',$id)->delete();
    if($result)
        return json(['state'=>'success','info'=>'删除成功']);
    else
        return json(['state'=>'error','info'=>'删除失败']);
}
# JSON链接交换排序
public function json_exc_txtlink_order(){
    if($this->request->isPost()){
        $user_id = Session::get('user.uid');
        #检查传入的两个交换值并进行交换操作
        $id1 = $this->request->param('id1/d');
        $id2 = substr($this->request->param('id2/s'), 2);
        $path1 = Db::name('link')->where('user_id',$user_id)->where('id',$id1)->value('link_order');
        $path2 = Db::name('link')->where('user_id',$user_id)->where('id',$id2)->value('link_order');
        if(($path1 != null) && ($path2 != null)){
            Db::name('link')->where('id',$id1)->update(['link_order'=>$path2]);
            Db::name('link')->where('id',$id2)->update(['link_order'=>$path1]);
            return json(['state'=>'success']);
        }else{
            return json(['state'=>'error','info'=>'您的ID选项不正确哦']);
        }
    }
}
# JSON设置分组
public function json_set_txtlink_group(){
    $user = Session::get('user');
    #检查并验证传入的两个ID值
    $id  = $this->request->param('id/d');
    $new_sec_id = $this->request->param('gid/d');
    if($new_sec_id > 0){
        $result_gid = Db::name('link_group')->where('uid',$user['uid'])->where('id',$new_sec_id)->findOrEmpty();
        if(empty($result_gid))
            return json(['state'=>'error','info'=>'父级栏目不存在哦']);
    }else if($new_sec_id == -1){
        $new_sec_id = null;
    }else{
        return json(['state'=>'error','info'=>'父级栏目输入错误哦']);
    }
    #设置分组
    $result = Db::name('link')->where('user_id',$user['uid'])->where('id',$id)->update(['far_id'=>$new_sec_id]);
    if($result)
        return json(['state'=>'success','info'=>'设置成功，刷新生效']);
    else
        return json(['state'=>'error','info'=>'设置分组失败']);
}
##########################################################################################################################################################




















################################################ 【没用的放进代码回收站】 #####################################################################################
#文字超链接分组首页
// public function txthypergroup(){
//     # 获取登录用户信息
//     $user = $this->get_user();

//     #获取此用户分组信息
//     $group_top = Db::name('link_group')->where('uid',$user['uid'])->where('far_id',0)->order('link_order desc')->select()->toArray();
//     $group_bot = Db::name('link_group')->where('uid',$user['uid'])->where('far_id','<>',0)->order('link_order desc')->select()->toArray();
//     foreach($group_top as $key => $vol){
//         foreach($group_bot as $k => $v){
//             if($vol['id']==$v['far_id']){
//                 $group_top[$key]['zclass'][] = $v;
//                 unset($group_bot[$k]);
//             }
//         }
//     }
//     View::assign([
//         'user'  => $user,
//         'group' => $group_top,
//         'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
//         'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
//         'menu' => null, // 首页导航ID
//     ]);
//     return View::fetch('hyperlink_txt_group');
// }
#图片超链接分组管理
// public function pichypergroup(){
//     # 获取登录用户信息
//     $user = $this->get_user();
//     View::assign([
//         'user'  => $user,
//         'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
//         'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
//         'menu' => null, // 首页导航ID
//         'email' => 'thinkphp@qq.com'
//     ]);
//     return View::fetch('hyperlink_pic_group');
// }
#图片超链接列表管理
// public function pichyperlist(){
//     # 获取登录用户信息
//     $user = $this->get_user();
//     View::assign([
//         'user'  => $user,
//         'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
//         'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
//         'menu' => null, // 首页导航ID
//         'email' => 'thinkphp@qq.com'
//     ]);
//     return View::fetch('hyperlink_pic_list');
// }
##########################################################################################################################################################
}